#include "Base.h"
#include "BaseProtocol.h"
#include "GameProtocol.h"
#include "MGProtocol.h"

BOOL IsBotUser(DWORD dwUserId)
{
    if(dwUserId < 100000)
		return TRUE;
	else
		return FALSE;
}

BOOL GMGameSvrInfoMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_szGSPubIP, sizeof(m_szGSPubIP));
	iStream.Read((char*)&m_nPort, sizeof(m_nPort));
	iStream.Read((char*)&m_dwGameId, sizeof(m_dwGameId));
	return TRUE;
}

BOOL GMGameSvrInfoMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_szGSPubIP, sizeof(m_szGSPubIP));
	oStream.Write((char*)&m_nPort, sizeof(m_nPort));
	oStream.Write((char*)&m_dwGameId, sizeof(m_dwGameId));
	return TRUE;
}

BOOL GMCreateMatchMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwGameId, sizeof(m_dwGameId));
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	iStream.Read((char*)&m_dwStageId, sizeof(m_dwStageId));
    return TRUE;
}

BOOL GMCreateMatchMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwGameId, sizeof(m_dwGameId));
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	oStream.Write((char*)&m_dwStageId, sizeof(m_dwStageId));
	return TRUE;
}

BOOL GMGameResultMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	iStream.Read((char*)&m_dwStageId, sizeof(m_dwStageId));
	iStream.Read((char*)&m_dwTableId, sizeof(m_dwTableId));
	int nCount;
	iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		UserGameResult userGameResult;
		iStream.Read((char*)&userGameResult, sizeof(UserGameResult));
		m_listUserGameResult.AddTail(userGameResult);
	}
	return TRUE;
}

BOOL GMGameResultMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	oStream.Write((char*)&m_dwStageId, sizeof(m_dwStageId));
	oStream.Write((char*)&m_dwTableId, sizeof(m_dwTableId));

	int nCount = m_listUserGameResult.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		UserGameResult &userGameResult = m_listUserGameResult.GetAt(i);
		oStream.Write((char*)&userGameResult, sizeof(UserGameResult));
	}
	return TRUE;
}

/*************************************************************/
/***********************MS->GS********************************/
/*************************************************************/
BOOL MGCreateMatchMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwGameId, sizeof(m_dwGameId));
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	iStream.Read((char*)&m_dwStageId, sizeof(m_dwStageId));
	int nCount = 0;
	iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		MatchAttr attr = {0};
		iStream.Read((char*)&attr, sizeof(attr));
		m_listMatchAttr.AddTail(attr);
	}

	iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		DWORD dwUserId = 0;
		iStream.Read((char*)&dwUserId, sizeof(dwUserId));
		m_listUserId.AddTail(dwUserId);
	}
	return TRUE;
}

BOOL MGCreateMatchMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwGameId, sizeof(m_dwGameId));
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	oStream.Write((char*)&m_dwStageId, sizeof(m_dwStageId));
	int nCount = m_listMatchAttr.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		MatchAttr &attr = m_listMatchAttr.GetAt(i);
		oStream.Write((char*)&attr, sizeof(attr));
	}

	nCount = m_listUserId.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		DWORD dwUserId = m_listUserId[i];
		oStream.Write((char*)&dwUserId, sizeof(dwUserId));
	}

	return TRUE;
}

BOOL MGEndMatchMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwGameId, sizeof(m_dwGameId));
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	
	return TRUE;
}

BOOL MGEndMatchMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwGameId, sizeof(m_dwGameId));
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_dwMatchId, sizeof(m_dwMatchId));

	return TRUE;
}

BOOL MGCreateGameMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwGameId, sizeof(m_dwGameId));
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	iStream.Read((char*)&m_dwStageId, sizeof(m_dwStageId));
	iStream.Read((char*)&m_dwTableId, sizeof(m_dwTableId));
	iStream.Read((char*)&m_TableRuler, sizeof(m_TableRuler));
	iStream.Read((char*)&m_szGameRuler, sizeof(m_szGameRuler));

	int nCount;
	iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		PlayerInfo *pPlayerInfo = new PlayerInfo;
		iStream.Read((char*)&pPlayerInfo->m_dwUserId, sizeof(pPlayerInfo->m_dwUserId));
		iStream.Read((char*)&pPlayerInfo->m_nSeat, sizeof(pPlayerInfo->m_nSeat));
        iStream.Read((char*)&pPlayerInfo->m_nScore, sizeof(pPlayerInfo->m_nScore));
		iStream.Read((char*)&pPlayerInfo->m_szUserName, sizeof(pPlayerInfo->m_szUserName));
		iStream.Read((char*)&pPlayerInfo->m_nFigure, sizeof(pPlayerInfo->m_nFigure));
		pPlayerInfo->m_GameAttrs.Read(iStream);

		m_listPlayerInfo.AddTail(pPlayerInfo);
	}
	return TRUE;
}

BOOL MGCreateGameMsg::Write( CSocketStream& oStream ) const
{
    oStream.Write((char*)&m_dwGameId, sizeof(m_dwGameId));
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	oStream.Write((char*)&m_dwStageId, sizeof(m_dwStageId));
	oStream.Write((char*)&m_dwTableId, sizeof(m_dwTableId));
	oStream.Write((char*)&m_TableRuler, sizeof(m_TableRuler));
	oStream.Write((char*)&m_szGameRuler, sizeof(m_szGameRuler));

	int nCount = m_listPlayerInfo.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		PlayerInfo *pPlayerInfo = m_listPlayerInfo.GetAt(i);
		oStream.Write((char*)&pPlayerInfo->m_dwUserId, sizeof(pPlayerInfo->m_dwUserId));
		oStream.Write((char*)&pPlayerInfo->m_nSeat, sizeof(pPlayerInfo->m_nSeat));
		oStream.Write((char*)&pPlayerInfo->m_nScore, sizeof(pPlayerInfo->m_nScore));
		oStream.Write((char*)&pPlayerInfo->m_szUserName, sizeof(pPlayerInfo->m_szUserName));
        oStream.Write((char*)&pPlayerInfo->m_nFigure, sizeof(pPlayerInfo->m_nFigure));
		pPlayerInfo->m_GameAttrs.Write(oStream);
	}
	return TRUE;
}

BOOL MGMatchInfoMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	int nCount = 0;
	iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		MatchAttr attr = {0};
        iStream.Read((char*)&attr, sizeof(attr));
		m_listMatchAttr.AddTail(attr);
	}
	return TRUE;
}

BOOL MGMatchInfoMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	int nCount = m_listMatchAttr.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		MatchAttr &attr = m_listMatchAttr.GetAt(i);
        oStream.Write((char*)&attr, sizeof(attr));
	}
	return TRUE;
}

BOOL MGMatchOrderMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_dwMatchId, sizeof(m_dwMatchId));
    int nCount;
	iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		UserOrder userOrder;
		iStream.Read((char*)&userOrder, sizeof(userOrder));
		m_listUserOrder.AddTail(userOrder);
	}
	return TRUE;
}

BOOL MGMatchOrderMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	int nCount = m_listUserOrder.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		UserOrder &userOrder = m_listUserOrder[i];
		oStream.Write((char*)&userOrder, sizeof(UserOrder));
	}
	return TRUE;
}

BOOL MGTipTextMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	iStream.Read((char*)&m_dwUserId, sizeof(m_dwUserId));
	iStream.Read((char*)&m_szTip, sizeof(m_szTip));
	return TRUE;
}

BOOL MGTipTextMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	oStream.Write((char*)&m_dwUserId, sizeof(m_dwUserId));
	oStream.Write((char*)&m_szTip, sizeof(m_szTip));
	return TRUE;
}

BOOL MGMatchAwardMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_dwProductId, sizeof(m_dwProductId));
	iStream.Read((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	iStream.Read((char*)&m_dwUserId, sizeof(m_dwUserId));
    m_stOrderAward.Read(iStream);
	return TRUE;
}

BOOL MGMatchAwardMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_dwProductId, sizeof(m_dwProductId));
	oStream.Write((char*)&m_dwMatchId, sizeof(m_dwMatchId));
	oStream.Write((char*)&m_dwUserId, sizeof(m_dwUserId));
	m_stOrderAward.Write(oStream);
	return TRUE;
}